<?php
/**
 * Created by PhpStorm.
 * User: DongYao
 * Date: 2017/12/1
 * Time: 下午4:47
 */

namespace XmtApp\Modules\Admin\Controllers\Api;


use App\Http\Requests\CategoryPost;
use App\Http\Resources\Categories;
use App\Tables\Categories as CategoriesTable;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use Overtrue\Pinyin\Pinyin;

class CategoryController
{
    /**
     * 获取类目
     * @param int $parent_id
     * @return Categories
     */
    public function categories($parent_id = 0, Request $request)
    {
        $table = new CategoriesTable();

        $table = $table->withTrashed()->where('parent_id', $parent_id);

        if ($request->has('page')) {
            $data = $table->paginate($request->header('pageSize'));

            $_data = $data->each(function ($item) {
                $item->status = !$item->trashed();
            });
            $data = new LengthAwarePaginator($_data, $data->total(), $data->perPage(), $data->currentPage());
        } else {
            $data = $table->get();
            $data->each(function ($item) {
                $item->status = $item->trashed();
            });
        }

        return new Categories($data);
    }

    /**
     * 添加类目
     * @param CategoryPost $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function add(CategoryPost $request)
    {
        $data = $request->all(['name', 'code', 'parent_id', 'sort']);

        $table = new CategoriesTable($data);

        $res = $table->save();
        if ($res) {
            return response()->json(ajax_return(true, '添加成功', $table->toArray()));
        } else {
            return response()->json(ajax_return(false, '添加失败'));
        }
    }

    /**
     * 修改类目
     * @param $id
     * @param CategoryPost $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function update($id, CategoryPost $request)
    {
        $data = $request->all(['name', 'code', 'parent_id', 'sort']);

        $table = new CategoriesTable();

        $res = $table->where('id', $id)->update($data);
        if ($res) {
            return response()->json(ajax_return(true, '修改成功', $table->toArray()));
        } else {
            return response()->json(ajax_return(false, '修改失败'));
        }
    }

    /**
     * 获取已选分类的父级类目
     * @param $id
     * @return mixed
     */
    public function selectedParents($id)
    {
        $tree = (new CategoriesTable)->getAllSons(0);
        $parents = (new CategoriesTable)->getParents($id, 1);
        return response()->json(ajax_return(true, 'ok', ['tree' => $tree, 'parents' => $parents]));
    }

    /**
     * 获取拼音标识
     * @param $name
     * @return string
     */
    public function formatCode($name)
    {
        if (empty($name)) {
            return response()->json(ajax_return(true, '生成成功', ''));
        }

        $pinyin = new Pinyin();
        $name = $pinyin->permalink($name, '');

        return response()->json(ajax_return(true, '生成成功', strtolower($name)));
    }

    /**
     * 设置类目是否禁用（删除）
     * @param $id
     * @param $status
     * @return \Illuminate\Http\JsonResponse
     */
    public function toggleStatus($id, $status)
    {
        $table = new CategoriesTable();

        $category = $table->withTrashed()->where('id', $id)->first();
        if (empty($category)) {
            return response()->json(ajax_return(false, '分类不存在'));
        }

        if ($category->trashed()) {
            $res = $category->restore();
        } else {
            $res = $category->delete();
        }

        if ($res) {
            return response()->json(ajax_return(true, '设置成功'));
        } else {
            return response()->json(ajax_return(false, '设置失败'));
        }
    }
}